const mysql = require('mysql');

function __connection () {

	// 创建mysql
	let connection = mysql.createConnection({
		host: 'localhost',
		user: 'root',
		password: 'root',
		database: 'koacms'
	});

	// 链接数据库
	connection.connect();

	return connection;
}

exports.query = function (sql, params) {

	params = params ? params : null;

	let connection = __connection();
	return new Promise((reject, resolve)=> {

		// 执行SQL操作
		connection.query(sql, params, (error, results, fields) => {
			if(error) resolve(error);
			reject(results);
		});

		// 关闭连接
		connection.end();
	});
};